from typing import List

from utils.storage import get_file_download_url


def serialize_permission(permission: 'Permission'):
    return {
        'id': permission.id,
        'name': permission.name,
        'type': permission.type
    }


def serialize_user(user: 'User', permissions: List['Permission'] = None) -> dict:
    return {
        'id': user.id,
        'username': user.username,
        'nickname': user.nickname,
        'first_name': user.first_name,
        'last_name': user.last_name,
        'skills': user.skills,
        'sex': user.sex,
        'avatar': get_file_download_url(user.avatar_store_id),
        'permissions': [serialize_permission(i) for i in permissions] if permissions else []
    }


def serialize_user_all_info(user: 'User', permissions: List['Permission'] = None) -> dict:
    data = serialize_user(user, permissions)
    data['work_experience'] = user.work_experience
    return data


def serialize_permission_group(permission_group: 'PermissionGroup', users: List['User'] = [], permissions: List['Permission'] = []):
    return {
        'id': permission_group.id,
        'name': permission_group.name,
        'users': [serialize_user(i) for i in users],
        'permissions': [serialize_permission(i) for i in permissions]
    }